#!/bin/sh

# run from directory where this script is
cd `dirname $0`
EXAMPLE_DIR=`pwd`

# check whether echo has the -e option
if test "`echo -e`" = "-e" ; then ECHO=echo ; else ECHO="echo -e" ; fi

$ECHO
$ECHO "$EXAMPLE_DIR : starting"
$ECHO
$ECHO "This example shows how to use pw.x with vdw-DF functional. In the"
$ECHO "first part a cell relaxation of graphite will be calculated and"
$ECHO "then the energy of two water molecules far apart will be computed."
$ECHO "Optionally, at the end, you can see how to set up a force relaxation"
$ECHO "of an Argon dimer, not activated by default in the distribution."

# set the needed environment variables
. ../../../environment_variables

# required executables and pseudopotentials
BIN_LIST="pw.x"
PSEUDO_LIST="C.pbe-rrkjus.UPF O.pbe-rrkjus.UPF H.pbe-rrkjus.UPF"

$ECHO
$ECHO "  executables directory: $BIN_DIR"
$ECHO "  pseudo directory:      $PSEUDO_DIR"
$ECHO "  temporary directory:   $TMP_DIR"
$ECHO "  checking that needed directories and files exist...\c"

# check for directories
for DIR in "$BIN_DIR" "$PSEUDO_DIR" ; do
    if test ! -d $DIR ; then
        $ECHO
        $ECHO "ERROR: $DIR not existent or not a directory"
        $ECHO "Aborting"
        exit 1
    fi
done
for DIR in "$TMP_DIR" "$EXAMPLE_DIR/results_dscf" ; do
    if test ! -d $DIR ; then
        mkdir $DIR
    fi
done
cd $EXAMPLE_DIR/results_dscf

# check for executables
for FILE in $BIN_LIST ; do
    if test ! -x $BIN_DIR/$FILE ; then
        $ECHO
        $ECHO "ERROR: $BIN_DIR/$FILE not existent or not executable"
        $ECHO "Aborting"
        exit 1
    fi
done

# check for pseudopotentials
for FILE in $PSEUDO_LIST ; do
    if test ! -r $PSEUDO_DIR/$FILE ; then
       $ECHO
       $ECHO "Downloading $FILE to $PSEUDO_DIR...\c"
            wget  http://www.quantum-espresso.org/pseudo/1.3/UPF/$FILE \
                -O $PSEUDO_DIR/$FILE 2> /dev/null
    fi
    if test $? != 0; then
        $ECHO
        $ECHO "ERROR: $PSEUDO_DIR/$FILE not existent or not readable"
        $ECHO "Aborting"
        exit 1
    fi
done
$ECHO " done"


# how to run executables
PW_COMMAND="$PARA_PREFIX $BIN_DIR/pw.x $PARA_POSTFIX"

# Print how we run executables
$ECHO
$ECHO "  running pw.x as: $PW_COMMAND"
$ECHO

# clean TMP_DIR
$ECHO "  cleaning $TMP_DIR...\c"
rm -rf $TMP_DIR/*
$ECHO " done"

#
# Graphite cell relaxation
#
#
cat > graphite.scf.0.in << EOF
&control
   calculation = "scf"
   restart_mode='from_scratch',
   prefix='graphite',
   tstress = .true.
   tprnfor = .true.
   pseudo_dir = '$PSEUDO_DIR',
   outdir='$TMP_DIR'
   verbosity = 'high'

   forc_conv_thr = 1.0D-3

/

&system
    ibrav           = 4
    celldm(1)       = 4.6411700000
    celldm(3)       = 2.7264000000

    nat             = 4
    ntyp            = 1

    occupations     = 'fixed'
    smearing        = 'methfessel-paxton'
    degauss         = 0.02

    ecutwfc         = 30.0
    ecutrho         = 180.0

    input_dft  	    = 'vdW-DF'
/

&electrons
    conv_thr        = 1.0d-8
/
ATOMIC_SPECIES
C   12.00   C.pbe-rrkjus.UPF

ATOMIC_POSITIONS {alat}
C      0.0000000000 0.0000000000 0.0000000000
C      0.0000000000 0.5773502692 0.0000000000
C      0.0000000000 0.0000000000 1.3632000000
C      0.5000000000 0.2886751346 1.3632000000

K_POINTS automatic
4 4 4 1 1 1
EOF

$ECHO "  running the graphite cell relaxation...\c"
$PW_COMMAND < graphite.scf.0.in > graphite.scf.0.out
check_failure $?
$ECHO " done"

#
cat > graphite.scf.+1.in << EOF
&control
   calculation = "scf"
   restart_mode='from_scratch',
   prefix='graphite',
   tstress = .true.
   tprnfor = .true.
   pseudo_dir = '$PSEUDO_DIR',
   outdir='$TMP_DIR'
   verbosity = 'high'

   forc_conv_thr = 1.0D-3

/

&system
    ibrav           = 4
    celldm(1)       = 4.6511700000
    celldm(3)       = 2.7264000000

    nat             = 4
    ntyp            = 1

    occupations     = 'fixed'
    smearing        = 'methfessel-paxton'
    degauss         = 0.02

    ecutwfc         = 29.87113860
    ecutrho         = 179.22683160

    input_dft  	    = 'vdW-DF'
/

&electrons
    conv_thr        = 1.0d-8
/
ATOMIC_SPECIES
C   12.00   C.pbe-rrkjus.UPF

ATOMIC_POSITIONS {alat}
C      0.0000000000 0.0000000000 0.0000000000
C      0.0000000000 0.5773502692 0.0000000000
C      0.0000000000 0.0000000000 1.3632000000
C      0.5000000000 0.2886751346 1.3632000000

K_POINTS automatic
4 4 4 1 1 1
EOF

$ECHO "  running the graphite cell relaxation...\c"
$PW_COMMAND < graphite.scf.+1.in > graphite.scf.+1.out
check_failure $?
$ECHO " done"

cat > graphite.scf.-1.in << EOF
&control
   calculation = "scf"
   restart_mode='from_scratch',
   prefix='graphite',
   tstress = .true.
   tprnfor = .true.
   pseudo_dir = '$PSEUDO_DIR',
   outdir='$TMP_DIR'
   verbosity = 'high'

   forc_conv_thr = 1.0D-3

/

&system
    ibrav           = 4
    celldm(1)       = 4.6311700000
    celldm(3)       = 2.7264000000

    nat             = 4
    ntyp            = 1

    occupations     = 'fixed'
    smearing        = 'methfessel-paxton'
    degauss         = 0.02

    ecutwfc         = 30.12969660
    ecutrho         = 180.77817960

    input_dft  	    = 'vdW-DF'
/

&electrons
    conv_thr        = 1.0d-8
/

ATOMIC_SPECIES
C   12.00   C.pbe-rrkjus.UPF

ATOMIC_POSITIONS {alat}
C      0.0000000000 0.0000000000 0.0000000000
C      0.0000000000 0.5773502692 0.0000000000
C      0.0000000000 0.0000000000 1.3632000000
C      0.5000000000 0.2886751346 1.3632000000

K_POINTS automatic
4 4 4 1 1 1
EOF

$ECHO "  running the graphite cell relaxation...\c"
$PW_COMMAND < graphite.scf.-1.in > graphite.scf.-1.out
check_failure $?
$ECHO " done"

#
# self-consistent calculation
# for water molecules
#
cat > water.scf.0.in << EOF
&control
   calculation = 'scf'
   restart_mode='from_scratch',
   prefix='water_vdw',
   tstress = .true.
   tprnfor = .true.
   pseudo_dir = '$PSEUDO_DIR/',
   outdir='$TMP_DIR/'
   verbosity = 'high'
/

&system
    ibrav           = 8
    celldm(1)       = 15.0
    celldm(2)       = 0.954545454545455
    celldm(3)       = 1.22727272727273
    nat             = 6
    ntyp            = 2
    occupations     = 'fixed'
    ecutwfc         = 30.0
    ecutrho         = 180.0
    input_dft       = 'vdW-DF'
/

&electrons
    conv_thr        = 1.0d-8
/

ATOMIC_SPECIES
   O   15.9994    O.pbe-rrkjus.UPF
   H   1.00794    H.pbe-rrkjus.UPF

ATOMIC_POSITIONS {alat}
O     0.0000000   0.0016540  -0.0072484
H     0.0000000   0.0981485  -0.0826521
H     0.0000000   0.0490883   0.1065556
O     0.0000000   0.1117595   0.3550478
H    -0.0975766   0.0656956   0.4133167
H     0.0975766   0.0656956   0.4133167



K_POINTS gamma

EOF

$ECHO "  running the scf calculation for water molecules...\c"
$PW_COMMAND < water.scf.0.in > water.scf.0.out
check_failure $?
$ECHO " done"

#
# self-consistent calculation
# for water molecules
#
cat > water.scf.+1.in << EOF
&control
   calculation = 'scf'
   restart_mode='from_scratch',
   prefix='water_vdw',
   tstress = .true.
   tprnfor = .true.
   pseudo_dir = '$PSEUDO_DIR/',
   outdir='$TMP_DIR/'
   verbosity = 'high'
/

&system
    ibrav           = 8
    celldm(1)       = 15.05
    celldm(2)       = 0.954545454545455
    celldm(3)       = 1.22727272727273
    nat             = 6
    ntyp            = 2
    occupations     = 'fixed'
    ecutwfc         = 29.80099530
    ecutrho         = 178.80597180
    input_dft       = 'vdW-DF'
/

&electrons
    conv_thr        = 1.0d-8
/

ATOMIC_SPECIES
   O   15.9994    O.pbe-rrkjus.UPF
   H   1.00794    H.pbe-rrkjus.UPF

ATOMIC_POSITIONS {alat}
O     0.0000000   0.0016540  -0.0072484
H     0.0000000   0.0981485  -0.0826521
H     0.0000000   0.0490883   0.1065556
O     0.0000000   0.1117595   0.3550478
H    -0.0975766   0.0656956   0.4133167
H     0.0975766   0.0656956   0.4133167

K_POINTS gamma

EOF

$ECHO "  running the scf calculation for water molecules...\c"
$PW_COMMAND < water.scf.+1.in > water.scf.+1.out
check_failure $?
$ECHO " done"

#
# self-consistent calculation
# for water molecules
#
cat > water.scf.-1.in << EOF
&control
   calculation = 'scf'
   restart_mode='from_scratch',
   prefix='water_vdw',
   tstress = .true.
   tprnfor = .true.
   pseudo_dir = '$PSEUDO_DIR/',
   outdir='$TMP_DIR/'
   verbosity = 'high'
/

&system
    ibrav           = 8
    celldm(1)       = 14.95
    celldm(2)       = 0.954545454545455
    celldm(3)       = 1.22727272727273
    nat             = 6
    ntyp            = 2
    occupations     = 'fixed'
    ecutwfc         = 30.20100420
    ecutrho         = 181.20602520
    input_dft       = 'vdW-DF'
/

&electrons
    conv_thr        = 1.0d-8
/

ATOMIC_SPECIES
   O   15.9994    O.pbe-rrkjus.UPF
   H   1.00794    H.pbe-rrkjus.UPF

ATOMIC_POSITIONS {alat}
O     0.0000000   0.0016540  -0.0072484
H     0.0000000   0.0981485  -0.0826521
H     0.0000000   0.0490883   0.1065556
O     0.0000000   0.1117595   0.3550478
H    -0.0975766   0.0656956   0.4133167
H     0.0975766   0.0656956   0.4133167

K_POINTS gamma

EOF

$ECHO "  running the scf calculation for water molecules...\c"
$PW_COMMAND < water.scf.-1.in > water.scf.-1.out
check_failure $?
$ECHO " done"

